﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace Pay.Core {
public class FtpHelper {
	private FtpHelper() { }
	private static FtpHelper _instance;
	private static readonly object obj = new object();
	public static FtpHelper GetInstance() {
		if (_instance == null) {
			lock (obj) {
				if (_instance == null) {
					_instance = new FtpHelper();
					return _instance;
				}
			}
		}
		return _instance;
	}

	public string[] GetFileName(string account, string pwd, string ftpPath) {
		string[] downloadFiles;
		FtpWebRequest reqFTP;
		try {
			reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
			reqFTP.UseBinary = true;
			reqFTP.Credentials = new NetworkCredential(account, pwd);
			reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
			reqFTP.UsePassive = false; //主动模式
			List<string> lst = new List<string>();
			using (WebResponse response = reqFTP.GetResponse()) {
				using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
					string line = reader.ReadLine();
					while (line != null) {
						lst.Add(line);
						line = reader.ReadLine();
					}
				}
			}

			return lst.ToArray();
		} catch (Exception ex) {
			downloadFiles = null;
			return downloadFiles;
		}
	}

	public bool Upload(string account, string pwd, string filename, string ftpPath, out string Msg) {
		bool isSuccess = false;
		Msg = string.Empty;
		FileInfo fileInf = new FileInfo(filename);
		FtpWebRequest reqFTP;
		// 根据uri创建FtpWebRequest对象   
		reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileInf.Name));
		// ftp用户名和密码  
		reqFTP.Credentials = new NetworkCredential(account, pwd);
		reqFTP.UsePassive = false;
		// 默认为true，连接不会被关闭  
		// 在一个命令之后被执行  
		reqFTP.KeepAlive = false;
		// 指定执行什么命令  
		reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
		// 指定数据传输类型  
		reqFTP.UseBinary = true;
		// 上传文件时通知服务器文件的大小  
		reqFTP.ContentLength = fileInf.Length;
		// 缓冲大小设置为2kb  
		int buffLength = 2048;
		byte[] buff = new byte[buffLength];
		int contentLen;
		// 打开一个文件流 (System.IO.FileStream) 去读上传的文件 
		try {
			using (FileStream fs = fileInf.OpenRead()) {
				// 把上传的文件写入流  
				using (Stream strm = reqFTP.GetRequestStream()) {
					// 每次读文件流的2kb  
					contentLen = fs.Read(buff, 0, buffLength);
					// 流内容没有结束  
					while (contentLen != 0) {
						// 把内容从file stream 写入 upload stream  
						strm.Write(buff, 0, contentLen);
						contentLen = fs.Read(buff, 0, buffLength);
					}
				}
			}
			isSuccess = true;
		} catch (Exception ex) {
			isSuccess = false;
			Msg = "程序出现异常：" + ex.ToString();
		}
		return isSuccess;
	}

	public bool Download(string account, string pwd, string ftpPath, string filePath, string fileName, out string Msg) {
		bool isSuccess = false;
		Msg = string.Empty;
		FtpWebRequest reqFTP;
		FtpWebRequest reqFTP1;
		try {
			using (FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create)) {
				reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
				reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
				reqFTP.UseBinary = true;
				reqFTP.Credentials = new NetworkCredential(account, pwd);
				reqFTP.UsePassive = false;
				long cl = 0;
				using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
					cl = response.ContentLength;
				}

				if (cl > 0) {
					reqFTP1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
					reqFTP1.Method = WebRequestMethods.Ftp.DownloadFile;
					reqFTP1.UseBinary = true;
					reqFTP1.Credentials = new NetworkCredential(account, pwd);
					reqFTP1.UsePassive = false;
					int bufferSize = 2048;
					int readCount;
					byte[] buffer = new byte[bufferSize];
					using (FtpWebResponse response1 = (FtpWebResponse)reqFTP1.GetResponse()) {
						using (Stream ftpStream = response1.GetResponseStream()) {
							readCount = ftpStream.Read(buffer, 0, bufferSize);
							while (readCount > 0) {
								outputStream.Write(buffer, 0, readCount);
								readCount = ftpStream.Read(buffer, 0, bufferSize);
								isSuccess = true;
							}
						}
					}
				}
			}
		} catch (Exception ex) {
			isSuccess = false;
			Msg = "程序出现异常：" + ex.Message;
		}
		return isSuccess;
	}

	public void Delete(string account, string pwd, string ftpPath, string fileName) {
		FtpWebRequest reqFTP;
		try {
			reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
			reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
			reqFTP.UseBinary = true;
			reqFTP.Credentials = new NetworkCredential(account, pwd);
			reqFTP.UsePassive = false;
			FtpWebResponse listResponse = (FtpWebResponse)reqFTP.GetResponse();
			string sStatus = listResponse.StatusDescription;
		} catch (Exception ex) {
			throw ex;
		}
	}
}
}
